﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
namespace LogicUniversity.clerks
{
    public partial class Disbursement_Detail : System.Web.UI.Page
    {
        RetrievalListController retController;
        List<Retrieval_List> result;

        public int Batch_No
        {
            get { return Convert.ToInt16(this.ViewState["batch_No"]); }
            set { this.ViewState["batch_No"] = value; }
        }
        public string Dept_Code
        {
            get { return (string)(this.ViewState["dept_Code"]); }
            set { this.ViewState["dept_Code"] = value; }
        }

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                result = new List<Retrieval_List>();
                retController = new RetrievalListController();
                Batch_No = PreviousPage.Batch_No;
                Dept_Code = PreviousPage.Dept_Code;
                result = retController.getDisbursementDetail(Batch_No, Dept_Code);

                grdDisbursementDetail.DataSource = result;
                grdDisbursementDetail.DataBind();
                this.ViewState["result"] = result;
            }
        }

        protected void btnComfirm_Click(object sender, EventArgs e)
        {
            Session["UserID"] = "A0102918";
            retController = new RetrievalListController();
            result = new List<Retrieval_List>();
            result = retController.getDisbursementDetail(Batch_No, Dept_Code);
            if (Session["UserID"] != null)
            {
                if (retController.updateDisbursementStatus(result, Session["UserID"].ToString()))
                {
                    Response.Redirect("~/clerks/Disbursement.aspx");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Session["UserID"] = "A0102918";
            retController = new RetrievalListController();
            Voucher_Detail adjustment = new Voucher_Detail();
            adjustment.Item_Code = lblItem_Code.Text;
            adjustment.Reason = txtReason.Text;
            try
            {
                int adjQty=Convert.ToInt16(txtAdjustmentQty.Text);
                int received = Convert.ToInt32(lblReceived.Text);

                if (adjQty > received)
                {
                    throw new Exception();
                }
                adjustment.Quantity_Adjusted = adjQty;
            }
            catch (Exception ex)
            {
                lblError.Text = "Illegal Qty";
                return;
            }
            if (Session["UserID"] != null)
            {
                retController.addAndUpdateAdjustment(adjustment, Batch_No, Dept_Code, Session["UserID"].ToString());
            }
                btnComfirm.Enabled = true;
            pnlDiscripency.Visible = false;
            result = new List<Retrieval_List>();
            result = retController.getDisbursementDetail(Batch_No,Dept_Code);
            this.ViewState["result"] = result;
            grdDisbursementDetail.DataSource= result;
            grdDisbursementDetail.DataBind();

        }
        
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ModifyReceive")
            {
                result = (List<Retrieval_List>)this.ViewState["result"];
                int row = Convert.ToInt16(e.CommandArgument);
                lblItem_Code.Text = result[row].Item_Code;
                lblReceived.Text = result[row].Actual_Quanty.ToString();
                btnComfirm.Enabled = false;
                pnlDiscripency.Visible = true;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlDiscripency.Visible = false;
            btnComfirm.Enabled = true;
        }
        protected void lnkBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Disbursement.aspx");
        }
    }
}